﻿using System;
using Core.Framework.Model.Common;
using Core.Framework.Model.WebSockets;
using Core.Framework.Redis;
using Core.Framework.Redis.Queue_Helper;
using Core.IBusiness.ILoggerModule;
using Core.Middleware.WebSockets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using Core.DataAccess.Model.Project.Queue;
using Core.Framework.EntityExtend;
using Microsoft.AspNetCore.Hosting;
using Core.Framework.Util;

namespace Core.WebSocketApi.Controllers
{
    /// <summary>
    /// 测试操作
    /// </summary>
    [Route("/")]
    [Route("api")]
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : BaseApiController
    {
        IApplicationLifetime applicationLifetime;


        public ValuesController(IApplicationLifetime appLifetime)
        {
            applicationLifetime = appLifetime;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object Get()
        {
            return WebSocketApplication.ClientsPool;
        }

        /// <summary>
        /// 当前队列消息
        /// </summary>
        /// <returns></returns>
        [HttpGet(".msg")]
        public ActionResult<object> Msg()
        {
            var redis = new RedisHelper(4);
            return redis.SortedGetRangeByRank<MessageQueue>(RedisQueueHelperParameter.Queue);
        }

        /// <summary>
        /// 队列服务客户机
        /// </summary>
        /// <returns></returns>
        [HttpGet(".sub")]
        public ActionResult<object> sub()
        {
            var redis = new RedisHelper();
            return redis.HashGet<List<Listener_Template>>(RedisQueueHelperParameter.QueueService,
                RedisQueueHelperParameter.ServiceClinet);
        }

        /// <summary>
        /// 卸载当前服务
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Uninstall")]
        public ActionResult<object> uni(string token)
        {

            if (!string.IsNullOrWhiteSpace(token))
            {
                if (token == CoreStartupHelper.GetConfigValue("Service:System"))
                {
                    applicationLifetime.StopApplication();
                    return "ApplicationStopping";
                }
            }

            return "error";
        }

    }
}
